package org.tangerinejunction.imstats.stats;

import java.util.Calendar;

import org.tangerinejunction.imstats.data.Account;
import org.tangerinejunction.imstats.data.Entity;
import org.tangerinejunction.imstats.util.TimeRange;

/**
 * EntityStatistics is a library of functions for calculating
 * various statistics on entities, usually over a particular time range.
 * 
 * @author meganthorsen
 */
public class EntityStatistics 
{
	/**
	 * 
	 * @param entity	the Entity to analyze
	 * @param range		the TimeRange over which to analyze the Entity
	 * @return			the total size of all files belonging to the Entity over the TimeRange
	 */
	public static long getFileSize(Entity entity, TimeRange range)
	{
		long totalSize = 0l;
		for (Account account : entity.getAccounts())
			totalSize += AccountStatistics.getFileSize(account, range);
		return totalSize;
	}
	
	/**
	 * 
	 * @param entity	the Entity to analyze
	 * @param range		the TimeRange over which to analyze the Entity
	 * @return			the total number of files belonging to the Entity over the TimeRange
	 */
	public static int getFileCount(Entity entity, TimeRange range)
	{
		int fileCount = 0;
		for (Account account : entity.getAccounts())
			fileCount += AccountStatistics.getFileCount(account, range);
		return fileCount;
				
	}
	
	/**
	 * 
	 * @param entity	the Entity to analyze
	 * @param range		the TimeRange over which to analyze the Entity
	 * @return			the first Calendar date logged for this Entity over the TimeRange
	 */
	public static Calendar getFirstLogDate(Entity entity, TimeRange range)
	{
		Calendar firstDate = range.getEnd();
		for (Account account : entity.getAccounts())
		{
			Calendar accountFirstDate = AccountStatistics.getFirstLogDate(account, range);
			if (accountFirstDate.before(firstDate))
				firstDate = accountFirstDate;
		}
		return firstDate;
	}
	
	/**
	 * 
	 * @param entity	the Entity to analyze
	 * @param range		the TimeRange over which to analyze the Entity
	 * @return			the last Calendar date logged for this Entity over the TimeRange
	 */
	public static Calendar getLastLogDate(Entity entity, TimeRange range)
	{
		Calendar lastDate = range.getBeginning();
		for (Account account : entity.getAccounts())
		{
			Calendar accountLastDate = AccountStatistics.getLastLogDate(account, range);
			if (accountLastDate.after(lastDate))
				lastDate = accountLastDate;
		}
		return lastDate;
	}

}
